﻿/******************************************************************
** 文 件 名: BD_ProjectLoanAmount_DAL.cs
** Copyright (c) 2010-2012 Honestysoft (GuangZhou) Co.,LTD
** 创 建 人: 此代码由生成器自动生成
** 创建日期: 2012-3-11 20:14:55
** 描    述: 实现公共部分的数据访问封装，提供项目贷款额度的数据访问类
** 版    本: 2.0.0.0
** 修改描述:
** 修 改 人:
** 修改日期:
******************************************************************/
using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using Honestysoft.BaseObject;
using Honestysoft.Financing.Model;

namespace Honestysoft.Financing.DataAccess
{
	/// <summary>
    /// BD_ProjectLoanAmount数据访问类
    /// </summary>
	public partial class BD_ProjectLoanAmount_DAL : BaseDataAccess<BD_ProjectLoanAmount>
    {		
		/// <summary>
        /// 构造函数，使用指定的连接字符串
        /// </summary>
        /// <param name="connectionStringName">连接字符串名称</param>
        public BD_ProjectLoanAmount_DAL(string connectionStringName)
            : base(connectionStringName)
        {

        }

        public DataTable GetProjectLoanAmount(string projectId, string projectName, string projectCategory, int? year, int pageSize, int currentPageIndex, out int pageCount, out int recordCount)
        {
            DbParameter pProjectID = CreateParameter("ProjectID");
            pProjectID.DbType = DbType.String;
            pProjectID.Value = projectId;
            DbParameter pProjectName = CreateParameter("Name");
            pProjectName.DbType = DbType.String;
            pProjectName.Value = projectName;
            DbParameter pProjectCategory = CreateParameter("ProjectCategory");
            pProjectCategory.DbType = DbType.String;
            pProjectCategory.Value = projectCategory;
            DbParameter pYear = CreateParameter("TheYear");
            pYear.DbType = DbType.Int32;
            if (year.HasValue) { pYear.Value = year.Value; }
            string sql = string.Format(@"Select A.*,B.Name ProjectName,B.SortCode
                From BD_ProjectLoanAmount A Inner Join
                BD_Project B On A.ProjectID=B.ID
                Where 1=1 {0} {1} {2} {3}",
                                  string.IsNullOrEmpty(projectId) ? "" : string.Format(" And A.ProjectID={0}", pProjectID.ParameterName),
                                  string.IsNullOrEmpty(projectName) ? "" : string.Format(" And B.Name Like '%'+{0}+'%'", pProjectName.ParameterName),
                                  string.IsNullOrEmpty(projectCategory) ? "" : string.Format(" And B.ProjectCategory={0}", pProjectCategory.ParameterName),
                                  year.HasValue ? string.Format(" And A.TheYear={0}", pYear.ParameterName) : "");
            DbCommand command = GetDbCommand(sql);
            if (!string.IsNullOrEmpty(projectId)) { command.Parameters.Add(pProjectID); }
            if (!string.IsNullOrEmpty(projectName)) { command.Parameters.Add(pProjectName); }
            if (!string.IsNullOrEmpty(projectCategory)) { command.Parameters.Add(pProjectCategory); }
            if (year.HasValue) { command.Parameters.Add(pYear); }
            return ExecuteDataTable(command, "SortCode,TheYear Desc,CreateTime Desc", pageSize, currentPageIndex, out pageCount, out recordCount);
        }

        /// <summary>
        /// 得到项目贷款额度
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public decimal GetProjectLoanAmount(string projectId, int year)
        {
            DbParameter pProject = CreateParameter("ProjectID");
            pProject.DbType = DbType.String;
            pProject.Value = projectId;
            DbParameter pYear = CreateParameter("year");
            pYear.DbType = DbType.Int32;
            pYear.Value = year;
            DbParameter pAmount = CreateParameter("loanAmount");
            pAmount.DbType = DbType.Decimal;
            pAmount.Direction = ParameterDirection.Output;
            DbCommand command = GetStoredProcCommand("GetProjectLoanAmount");
            command.Parameters.Add(pProject);
            command.Parameters.Add(pYear);
            command.Parameters.Add(pAmount);
            ExecuteNonQuery(command);
            return Convert.ToDecimal(pAmount.Value);
        }
    }
}
